import time
import json
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
import base64
import urllib

import time

from selenium import webdriver
from selenium.webdriver import ActionChains

browser = webdriver.Chrome()
browser.get('http://127.0.0.1:8080/register')
# 获取用户名输入框
user = browser.find_elements_by_tag_name('input')[1]
user.send_keys('1122')
# user = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/div[1]/input')
# user.send_keys('1122')
time.sleep(2)
# 获取密码输入框
browser.find_elements_by_tag_name('input')[2].send_keys('111')
# pas = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/div[2]/input')
# pas.send_keys('111')

# 获取密码输入框
browser.find_elements_by_tag_name('input')[3].send_keys('111')
# pas = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/div[2]/input')
# pas.send_keys('111')


time.sleep(2)
# 获取图片
image = browser.find_element_by_xpath('//*[@id="app"]/div/section/div/div[4]/img')
image.screenshot('code.png')





res = requests.post('https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Ofvb6QhGI4q3v6CmWFba2Dpv&client_secret=AobGhNtFgvPcKg6G0CzGKWU81m5h6wGy')



res = json.loads(str(res.text))

token = res['access_token']

url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + token

header = {'Content-Type': 'application/x-www-form-urlencoded'}

img = open('./code.png', 'rb')
temp_img = img.read()
img.close()

data = {
    'image': base64.b64encode(temp_img)
}
data = urllib.parse.urlencode(data)

res = requests.post(url=url, data=data, headers=header)
print(res.text)
mycode = json.loads(res.text)['words_result']
print(mycode)
code = ''
for i in mycode:
    code += str(i['words'])
    
    
print(code)


# 获取验证码输入框
browser.find_elements_by_tag_name('input')[4].send_keys(code)



browser.find_element_by_xpath('//*[@id="app"]/div/section/div/button').click()